package com.wtsw.pulsosocial.util.facebook;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.wtsw.pulsosocial.R;
import com.wtsw.pulsosocial.controller.GestorUsuario;
import com.wtsw.pulsosocial.thread.CategoriasThread;

public class MainFragment extends Fragment {	
	private static final String TAG = "MainFragment";
	private UiLifecycleHelper uiHelper;	
	private ProgressDialog pDialog;
	private Activity activity;
	private Session.StatusCallback callback = new Session.StatusCallback() {
	    @Override
	    public void call(Session session, SessionState state, Exception exception) {
	        onSessionStateChange(session, state, exception);
	    }
	};	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    this.activity = getActivity();
	    this.pDialog = new ProgressDialog(activity);
	    uiHelper = new UiLifecycleHelper(activity, callback);
	    uiHelper.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, 
	        ViewGroup container, 
	        Bundle savedInstanceState) {
	    View view = inflater.inflate(R.layout.activity_fullscreen, container, false);

	    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
	    authButton.setFragment(this);
	    
	    return view;
	}
	private void onSessionStateChange(Session session, SessionState state, Exception exception) {
	    if (state.isOpened()) {
	        Log.i(TAG, "Logged in...");	        
	         Request.executeMeRequestAsync(session,
	         		 new Request.GraphUserCallback() {		
	                 	@Override
	                 	public void onCompleted(GraphUser user,Response response) {
	                 		if (user != null) {		
	                 			//Log.i(TAG,"FB User ID "+ user.getId());	
	                            Log.i(TAG,"FB Email "+ user.asMap().get("email"));
	                            GestorUsuario.cargarUsuario(user);	                            
	                        }		
	                    }		
	                 });	
	         levantar_estructuras();	              
	    }		        
	    else if (state.isClosed()) {
	        Log.i(TAG, "Logged out...");	 
	    }
	}
	
	
	
	public void levantar_estructuras() {
		// TODO Auto-generated method stub
		new CategoriasThread(activity,this).execute();	 
	}
	@Override
	public void onResume() {
	    super.onResume();
	    uiHelper.onResume();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    uiHelper.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onPause() {
	    super.onPause();
	    uiHelper.onPause();
	}

	@Override
	public void onDestroy() {
	    super.onDestroy();
	    uiHelper.onDestroy();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
	    super.onSaveInstanceState(outState);
	    uiHelper.onSaveInstanceState(outState);
	}
	public ProgressDialog getpDialog() {
		return pDialog;
	}
	public void setpDialog(ProgressDialog pDialog) {
		this.pDialog = pDialog;
	}
	
	public Activity getMainFragmentActivity() {
		return activity;
	}


}
